/*
* @Author: steven.wang
* @Date:   2019-03-21 14:01:06
* @Last Modified by:   steven.wang
* @Last Modified time: 2019-05-13 14:44:58
*/
const db = require('../db');

/**
 * 查询行政区块列表
 * @author steven.wang
 * @DateTime 2019-03-21T14:01:28+0800
 */
module.exports.list = values => {
	let data = {
		wants: '*',
		action: 'search',
		table: values.type
	};
	if (values.page) {
		data.pages = {
			page: parseInt(values.page) || 1,
			pageSize: parseInt(values.pageSize) || 10
		};
	}
	const dbSql = db.generateSql(data);
	return db.query(dbSql.sql, dbSql.sqlData);
}
/**
 * 获取行政区块总数
 * @author steven.wang
 * @DateTime 2019-03-21T16:46:39+0800
 */
module.exports.count = values => {
	const dbSql = db.generateSql({
		wants: 'count(*)',
		action: 'search',
		table: values.type
	});
	console.log(dbSql);
	return db.query(dbSql.sql, dbSql.sqlData);
}
/**
 * 根据省份code获取其城市列表
 * @author steven.wang
 * @DateTime 2019-03-21T17:04:44+0800
 */
module.exports.getCitysByProvinceCode = values => {
	const dbSql = db.generateSql({
		wants: '*',
		action: 'search',
		table: 'city',
		conditions: {
			parent_code: [values.code]
		}
	});
	return db.query(dbSql.sql, dbSql.sqlData);
}
/**
 * 根据城市code获取县/区列表
 * @author steven.wang
 * @DateTime 2019-03-22T14:46:57+0800
 */
module.exports.getCountysByCityCode = values => {
	const dbSql = db.generateSql({
		wants: '*',
		action: 'search',
		table: 'county',
		conditions: {
			parent_code: [values.code]
		}
	});
	console.log(values.code, dbSql)
	return db.query(dbSql.sql, dbSql.sqlData);
}
/**
 * 根据县/区code获取乡镇列表
 * @author steven.wang
 * @DateTime 2019-03-22T15:24:58+0800
 */
module.exports.getTownsByCountyCode = values => {
	const dbSql = db.generateSql({
		wants: '*',
		action: 'search',
		table: 'town',
		conditions: {
			parent_code: [values.code]
		}
	});
	console.log(values.code, dbSql)
	return db.query(dbSql.sql, dbSql.sqlData);
}

/**
 * 根据乡镇code获取社区列表
 * @author steven.wang
 * @DateTime 2019-03-22T15:38:31+0800
 */
module.exports.getVillagesByTownCode = values => {
	const dbSql = db.generateSql({
		wants: '*',
		action: 'search',
		table: 'village',
		conditions: {
			parent_code: [values.code]
		}
	});
	console.log(values.code, dbSql)
	return db.query(dbSql.sql, dbSql.sqlData);
}